﻿/***************************************************************************************/
// Author                 - Balaji Venkatesan
// Student ID             - 12527079
// Version                - 1.0
// CodeName               - Northodyne Solutions
// Module                 - E-Business Technologies
// Date                   - 12-September-2011
/***************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class AddProperty : System.Web.UI.Page
{
    public string _connStr = "Initial Catalog=DBNorthodyneSolutions; Data Source=ABC\\SQLExpress;Trusted_Connection=Yes";
    public string _userName = string.Empty;

    public string _propertyID = "";
    public string _nearestMRT = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        _userName = Session["UserName"].ToString();
        //lblUserName.Text = _userName;

        Label lblusername = (Label)Master.FindControl("lblUserName");
        HyperLink hyplogin = (HyperLink)Master.FindControl("hyplogin");
        lblusername.Text = _userName;
    }

    protected void btnAddProperty_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection(_connStr))
        {
            connection.Open();

            //string agentIDQuery = "SELECT AgentID from Tb_Agents WHERE AreaAllocated = @AreaAllocated";
            
            //SqlCommand agentIDCmd = new SqlCommand(agentIDQuery, connection);
            //SqlParameter[] IDqueryPrms = new SqlParameter[1];
            //IDqueryPrms[0] = new SqlParameter("@AreaAllocated", SqlDbType.NVarChar, 50);
            //IDqueryPrms[0].SqlValue = txtNearestMRT.Text.Trim();
            //agentIDCmd.Parameters.AddRange(IDqueryPrms);
            //string agentID = Convert.ToString(agentIDCmd.ExecuteScalar());


            string propertyIDKey = "SELECT max(PropertyID) from Tb_Property";
            SqlCommand propertyIDCmd = new SqlCommand(propertyIDKey, connection);
            _propertyID = Convert.ToString(propertyIDCmd.ExecuteScalar());

            int ActualPropertyID = Convert.ToInt32(_propertyID.Substring(1));
            ActualPropertyID++;
            string resultantKey = "A" + ActualPropertyID;

            string insertProperty = "INSERT INTO Tb_Property (PropertyID, PropertyType, TransactionType, Rooms, Concourse, SwimmingPool, CarParking, Gym, Playground, PostedDate, Owner, Cost, AgentName, AgentContact, FullAddress, NearestMRT, Favourites) VALUES " + "(@PropertyID,@PropertyType,@TransactionType,@Rooms,@Concourse,@SwimmingPool,@CarParking,@Gym,@Playground,@PostedDate,@Owner,@Cost,@AgentName,@AgentContact,@FullAddress,@NearestMRT,@Favourites)";
            using (SqlCommand addPropertyCmd = new SqlCommand(insertProperty, connection))
            {
                SqlParameter[] prms = new SqlParameter[17];
                prms[0] = new SqlParameter("@PropertyID", SqlDbType.NVarChar, 20);
                prms[0].SqlValue = resultantKey;//ActualPropertyID;
                prms[1] = new SqlParameter("@PropertyType", SqlDbType.NVarChar, 50);
                prms[1].SqlValue = ddlPropertyType.SelectedItem.Text.Trim();
                prms[2] = new SqlParameter("@TransactionType", SqlDbType.NVarChar, 50);
                prms[2].SqlValue = txtTransactionType.Text.Trim();

                //Cross check this piece of code
                prms[3] = new SqlParameter("@Rooms", SqlDbType.Int);
                prms[3].SqlValue = txtRooms.Text.Trim();
                //

                prms[4] = new SqlParameter("@Concourse", SqlDbType.NVarChar, 50);
                prms[4].SqlValue = rblstConcourse.SelectedItem.Text.Trim(); //chklstConcourse.SelectedItem.Text.Trim();
                prms[5] = new SqlParameter("@SwimmingPool", SqlDbType.NVarChar,50);
                prms[5].SqlValue = rblstSwimmingPool.SelectedItem.Text.Trim(); //chklstSwimmingPool.SelectedItem.Text.Trim();
                prms[6] = new SqlParameter("@CarParking", SqlDbType.NVarChar, 50);
                prms[6].SqlValue = rblstcarParking.SelectedItem.Text.Trim();
                prms[7] = new SqlParameter("@Gym", SqlDbType.NVarChar, 50);
                prms[7].SqlValue = rblstGym.SelectedItem.Text.Trim();
                prms[8] = new SqlParameter("@Playground", SqlDbType.NVarChar, 50);
                prms[8].SqlValue = rblstPlayground.SelectedItem.Text.Trim();


                prms[9] = new SqlParameter("@PostedDate", SqlDbType.Date);
                prms[9].SqlValue = System.DateTime.Now.ToShortDateString();
                
                prms[10] = new SqlParameter("@Owner", SqlDbType.NVarChar, 50);
                prms[10].SqlValue = txtOwner.Text.Trim();
                prms[11] = new SqlParameter("@Cost", SqlDbType.NVarChar, 50);
                prms[11].SqlValue = txtCost.Text.Trim();

                prms[12] = new SqlParameter("@AgentName", SqlDbType.NVarChar, 50);
                prms[12].SqlValue = txtagentName.Text.Trim();

                prms[13] = new SqlParameter("@AgentContact", SqlDbType.NVarChar, 20);
                prms[13].SqlValue = txtAgentContactNumber.Text.Trim();
                prms[14] = new SqlParameter("@FullAddress", SqlDbType.NVarChar, 50);
                prms[14].SqlValue = txtFullAddress.Text.Trim();
                prms[15] = new SqlParameter("@NearestMRT", SqlDbType.NVarChar, 50);
                prms[15].SqlValue = txtNearestMRT.Text.Trim();

                prms[16] = new SqlParameter("@Favourites", SqlDbType.NVarChar, 50);
                prms[16].SqlValue = "false";

                addPropertyCmd.Parameters.AddRange(prms);
                addPropertyCmd.ExecuteNonQuery();
            }

            connection.Close();
        }

        Response.Redirect("AdminViewProperty.aspx");
    }
}